package com.shy.config.model;

import com.shy.config.common.BusinessEnum;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * 统一响应数据封装类
 */
@AllArgsConstructor
@NoArgsConstructor
@Builder
@Data
public class R<T> {
    /**
     * 状态码
     */
    private Integer code;
    /**
     * 响应信息
     */
    private String msg;

    /**
     * 响应数据
     */
    private T data;

    /**
     * 操作成功
     * @param data 响应数据
     * @return R
     * @param <T> 泛型
     */
    public static <T> R<T> success(T data) {
        return R
                .<T>builder()
                .code(BusinessEnum.SUCCESS.getCode())
                .msg(BusinessEnum.SUCCESS.getMsg())
                .data(data)
                .build();
    }

    /**
     * 操作失败
     * @return R
     * @param <T> 泛型
     */
    public static <T> R<T> failure() {
        return R
                .<T>builder()
                .code(BusinessEnum.SUCCESS.getCode())
                .msg(BusinessEnum.SUCCESS.getMsg())
                .build();
    }
    /**
     * 操作失败
     * @return R
     * @param <T> 泛型
     */
    public static <T> R<T> failure(BusinessEnum businessEnum) {
        return R
                .<T>builder()
                .code(businessEnum.getCode())
                .msg(businessEnum.getMsg())
                .data(null)
                .build();
    }

    /**
     * 操作失败
     * @param msg 错误信息
     * @return R
     * @param <T> 泛型
     */
    public static <T> R<T> failure(String msg) {
        return R
                .<T>builder()
                .code(BusinessEnum.FAILURE.getCode())
                .msg(msg)
                .data(null)
                .build();
    }
    public static <T> R<T> HANDLE(Boolean flag){
        if (flag) {
            return R.<T>builder()
                    .code(BusinessEnum.SUCCESS.getCode())
                    .msg(BusinessEnum.SUCCESS.getMsg())
                    .data(null)
                    .build();
        }
        return R.<T>builder()
                .code(BusinessEnum.SUCCESS.getCode())
                .msg(BusinessEnum.FAILURE.getMsg())
                .data(null).build();
    }
}
